package com.longfor.dragonshard.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.longfor.dragonshard.dto.log.LogSearchListDTO;
import com.longfor.dragonshard.entity.Log;
import com.longfor.dragonshard.mapper.LogMapper;
import com.longfor.dragonshard.service.ILogService;
import com.longfor.dragonshard.service.base.AbstractBaseService;
import com.longfor.drogon.common.LFConstants;
import com.longfor.drogon.common.bean.LFDatatableResultBean;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author wangyong
 * @version v1.0
 * @create 2018-01-25 22:06
 **/
@Service
@CacheConfig(cacheNames = LFConstants.CACHE.DEFAULT_CACHE_NAME)
public class LogServiceImpl extends AbstractBaseService<Log> implements ILogService {
    //private Logger logger = LoggerFactory.getLogger(CatalogServiceImpl.class);

    @Autowired
    private LogMapper logMapper;

    @Override
    @Cacheable(key = "#p0")
    public Optional<Log> selectById(Long id) {
        return Optional.ofNullable(logMapper.selectByPrimaryKey(id));
    }

    @Override
    public Long insert(Log log) {
        logMapper.insert(log);
        return log.getId();
    }

    @Override
    @CachePut(key = "#p0.id")
    public void update(Log log) {
        logMapper.updateByPrimaryKey(log);
    }

    @Override
    @CacheEvict(key = "#p0")
    public Integer delete(Long id) {
        return logMapper.deleteByPrimaryKey(id);
    }

    @Override
    @SuppressWarnings("unchecked")
    public LFDatatableResultBean searchList(LogSearchListDTO dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize(), true);
        Example example = new Example(Log.class);
        Example.Criteria c = example.createCriteria();
        System.out.println("======BBBBBBB=====");
        System.out.println(dto.getStart());
        if (dto.getStart() != null) {
            c.andGreaterThanOrEqualTo("operateTime", dto.getStart());
        }
        if (dto.getEnd() != null) {
            // 日期参数为yyyy-MM-dd，包含结束日期数据需要+1天
            Date end = DateUtils.addDays(dto.getEnd(), 1);
            c.andLessThan("operateTime", end);
        }
        if (StringUtils.isNotEmpty(dto.getUsername())) {
            c.andLike("username", "%" + dto.getUsername() + "%");
        }
        if (StringUtils.isNotEmpty(dto.getType())) {
            c.andEqualTo("type", dto.getType());
        }
        if (StringUtils.isNotEmpty(dto.getModule())) {
            c.andLike("module", dto.getModule());
        }
        example.setOrderByClause("operate_time desc");
        List<Log> list = mapper.selectByExample(example);
        PageInfo p = new PageInfo(list);
        return new LFDatatableResultBean(p);
    }
}
